package com.itheima.tcpdemo02;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        //TCP Socket 客服端
        //1.创建 Socket 对象
        Socket socket = new Socket("127.0.0.1", 10002);

        //2.获取 OutputStream
        OutputStream os = socket.getOutputStream();
        //3.写入数据
        String str = "你好";
        os.write(str.getBytes());

        //6.向服务端发送结束标记
        socket.shutdownOutput();

        //5.接收服务端发送的数据
        InputStream stream = socket.getInputStream();
        InputStreamReader reader = new InputStreamReader(stream);

        int b;

        while ((b = reader.read()) != -1) {
            System.out.print((char) b);
        }

        //4.释放
        socket.close();

    }
}
